package com.lkd.http.controller;

import com.github.wxpay.plus.WXConfig;
import com.github.wxpay.plus.WxPayTemplate;
import com.lkd.exception.LogicException;
import com.lkd.service.ESOrderService;
import com.lkd.service.OrderService;
import com.lkd.vo.OrderVO;
import com.lkd.vo.Pager;
import com.lkd.vo.PayReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private WxPayTemplate wxPayTemplate;

    @Autowired
    private ESOrderService esOrderService;


    /**
     * 微信支付
     * @param payVO
     * @return
     */
    @PostMapping("/weixinPay")
    public Map<String, String> weixinPay(@RequestBody PayReq payVO){
        return orderService.createOrderAndWxPayOrder(payVO);//创建订单
    }

    /**
     * 微信支付回调接口
     * @param request
     * @return
     */
    @RequestMapping("/payNotify")
    public void payNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {

        log.info("调用了回调方法");
        String resultMsg = WXConfig.successMsg();

        try {
            Map<String, String> result = wxPayTemplate.validPay(request.getInputStream());
            orderService.processPaymentResult(result);
        }catch (LogicException e){
            log.error("支付回调处理失败",e);
            resultMsg = WXConfig.failureMsg(e.getMessage());
        }catch (Exception e){
            log.error("支付回调处理失败",e);
            resultMsg = WXConfig.failureMsg("支付通知处理异常");
        }

        //给微信支付一个成功的响应
        response.setContentType("text/xml");
        response.getWriter().write(resultMsg);//给微信一个成功的响应
    }


    /**
     * 订单查询
     * @param pageIndex
     * @param pageSize
     * @param orderNo
     * @param openId
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/search")
    public Pager<OrderVO> search(
            @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
            @RequestParam(value = "orderNo",required = false,defaultValue = "") String orderNo,
            @RequestParam(value = "openId",required = false,defaultValue = "") String openId,
            @RequestParam(value = "startDate",required = false,defaultValue = "") String startDate,
            @RequestParam(value = "endDate",required = false,defaultValue = "") String endDate){
        return esOrderService.search(pageIndex,pageSize,orderNo,openId,startDate,endDate);
    }


    /**
     * 获取商圈下3个月内销量前10商品
     * @param businessId
     * @return
     */
    @GetMapping("/businessTop10/{businessId}")
    public List<Long> getBusinessTop10Skus(@PathVariable Integer businessId){
        return null;
    }




}
